package com.itsq.service.resources.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itsq.common.bean.ErrorEnum;
import com.itsq.common.constant.APIException;
import com.itsq.pojo.dto.PageBannerDto;
import com.itsq.pojo.entity.Banner;
import com.itsq.mapper.BannerMapper;
import com.itsq.service.resources.BannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 史先帅
 * @since 2021-01-29
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Autowired(required = false)
    private BannerMapper bannerMapper;


    @Override
    public Page<Banner> selBannerPageFront(PageBannerDto pageBannerDto) {
        Page<Banner> page=new Page<>(pageBannerDto.getPageIndex(),pageBannerDto.getPageSize());
        QueryWrapper queryWrapper=new QueryWrapper();
        if(pageBannerDto.getOrderByField()!=null){
            queryWrapper.orderByDesc(pageBannerDto.getOrderByField());
        }else{
            queryWrapper.orderByDesc("weight");
        }
        return bannerMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Page<Banner> selBannerPageAfter(PageBannerDto pageBannerDto) {
        Page<Banner> page=new Page<>(pageBannerDto.getPageIndex(),pageBannerDto.getPageSize());
        QueryWrapper queryWrapper=new QueryWrapper();
        if(pageBannerDto.getOrderByField()!=null){
            queryWrapper.orderByDesc(pageBannerDto.getOrderByField());
        }else{
            queryWrapper.orderByDesc("weight");
        }
        return bannerMapper.selectPage(page, queryWrapper);
    }

    @Override
    @Transactional
    public Integer insert(Banner banner) {
        int i = bannerMapper.insert(banner);
        if(i<1){
            throw new APIException(ErrorEnum.is_ISNULL);
        }
        return i;
    }

    @Override
    @Transactional
    public Integer update(Banner banner) {
        int i = bannerMapper.updateById(banner);
        if(i<1){
            throw new APIException(ErrorEnum.is_ISNULL);
        }
        return i;
    }
}
